<?php
/**
 * Created by PhpStorm.
 * User: liugang
 * Date: 2019-03-30
 * Time: 16:14
 */

namespace App\Services;


class OrderService extends BaseService
{
    // 今日货源
    const ORDER_TODAY = 1;

    // 订阅货源
    const ORDER_SUBSCRIBED = 2;

    private $allowedCodes = [
        1 => '%s_province_code',
        2 => '%s_city_code',
        3 => '%s_district_code',
    ];

    /**
     * 区域查询
     *
     * @param $builder
     * @param $srcCode
     * @param $srcCodeType
     * @param $dstCode
     * @param $dstCodeType
     * @return mixed
     * @throws \Exception
     */
    public function sectionQueryBuilder($builder, $srcCode, $srcCodeType, $dstCode, $dstCodeType)
    {
        if (!$srcCode && !$dstCode) {
            return $builder;
        }

        if ($srcCode && (!$srcCodeType || !in_array($srcCodeType, array_keys($this->allowedCodes)))) {
            throw new \Exception('出发地code类型为空或非法');
        }
        if ($dstCode && (!$dstCodeType || !in_array($dstCodeType, array_keys($this->allowedCodes)))) {
            throw new \Exception('目的地code类型为空或非法');
        }

        $srcCode && $builder = $builder->where(sprintf($this->allowedCodes[$srcCodeType], 'src'), $srcCode);
        $dstCode && $builder = $builder->where(sprintf($this->allowedCodes[$dstCodeType], 'dst'), $dstCode);

        return $builder;
    }

}